// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1

package file

import (
	"audio-server/internal/model"
	"audio-server/internal/sqlutil"
	"context"
	"encoding/json"

	"audio-server/internal/svc"
	"audio-server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileDetailLogic {
	return &FileDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileDetailLogic) FileDetail(req *types.FileDeleteReq) (resp *types.FileRes, err error) {
	id := req.FileId
	fileModel := model.NewSysFileModel(l.svcCtx.Conn)

	file, err := fileModel.FindOne(l.ctx, id)
	if err != nil {
		return &types.FileRes{}, err
	}
	res := types.FileRes{
		FileId:     file.FileId,
		Name:       file.Name,
		ExtendName: file.ExtendName.String,
		IsDir:      file.IsDir.Int64,
		ParentId:   file.ParentId,
		Size:       float64(file.Size),
		Src:        sqlutil.NullStringToString(file.Src),
		UpdateTime: file.UpdateTime.Format("2006-01-02 15:04:05"),
	}
	var strSlice []string
	tags := sqlutil.NullStringToString(file.Tags)
	if tags != "" {
		err := json.Unmarshal([]byte(tags), &strSlice)
		if err == nil {
			if len(strSlice) != 0 {
				res.Tags = strSlice
			}
		}

	}
	return &res, nil
}
